using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.Build;
using UnityEngine;

namespace UnityXEditor
{
    [Serializable]
    public class WeChatSettings
    {
        [SerializeField]
        private bool m_Enabled = false;
        [SerializeField]
        public string m_AndroidAppId = "";
        [SerializeField]
        public string m_AndroidAppSecret = "";
        [SerializeField]
        public string m_iOSAppId = "";
        [SerializeField]
        public string m_iOSAppSecret = "";
        [SerializeField]
        public string m_iOSUniversalLink = "";
        [SerializeField]
        public string m_iOSApplinks = "";

        public bool Enabled
        {
            get { return m_Enabled; }
            set
            {
                if (value != m_Enabled)
                {
                    m_Enabled = value;
                    Update();
                }
            }
        }
        public void OnGUI()
        {
            Enabled = EditorGUILayout.ToggleLeft("微信SDK", Enabled);
            if (Enabled)
            {
                EditorGUI.indentLevel += 2;
                m_AndroidAppId = EditorGUILayout.TextField("Android AppId", m_AndroidAppId);
                m_AndroidAppSecret = EditorGUILayout.TextField("Android AppSecret", m_AndroidAppSecret);
                m_iOSAppId = EditorGUILayout.TextField("iOS AppId", m_iOSAppId);
                m_iOSAppSecret = EditorGUILayout.TextField("iOS AppSecret", m_iOSAppSecret);
                m_iOSUniversalLink = EditorGUILayout.TextField("iOS UniversalLink", m_iOSUniversalLink);
                m_iOSApplinks = EditorGUILayout.TextField("iOS Applinks", m_iOSApplinks);
                EditorGUI.indentLevel -= 2;
            }
        }
        public void Update()
        {
            CheckManifest();
            UpdateCSFile();
            UpdatePlugin();

            UpdateTarget(NamedBuildTarget.Android);
            UpdateTarget(NamedBuildTarget.iOS);
        }

        private void CheckManifest()
        {
            if (Enabled)
            {
                string projectSettings = File.ReadAllText("ProjectSettings/ProjectSettings.asset");
                if (!projectSettings.Contains("useCustomMainManifest: 1"))
                {
                    Debug.LogError("请在ProjectSettings/Android里面勾选：自定义主要清单");
                }
            }
        }
        private void UpdateCSFile()
        {
            string cspath = @"Assets/UnityX/Runtime/WeChat/WeChat.cs";
            //AssetDatabase.FindAssets("WeChat");
            string csfile = File.ReadAllText(cspath);
            csfile = Regex.Replace(csfile, @"public const string AndroidAppId = .*;", "public const string AndroidAppId = \"" + m_AndroidAppId + "\";");
            csfile = Regex.Replace(csfile, @"public const string AndroidAppSecret = .*;", "public const string AndroidAppSecret = \"" + m_AndroidAppSecret + "\";");
            csfile = Regex.Replace(csfile, @"public const string iOSAppId = .*;", "public const string iOSAppId = \"" + m_iOSAppId + "\";");
            csfile = Regex.Replace(csfile, @"public const string iOSAppSecret = .*;", "public const string iOSAppSecret = \"" + m_iOSAppSecret + "\";");
            csfile = Regex.Replace(csfile, @"public const string iOSUniversalLink = .*;", "public const string iOSUniversalLink = \"" + m_iOSUniversalLink + "\";");
            File.WriteAllText(cspath, csfile);
        }
        private void UpdatePlugin()
        {

        }


        private void UpdateTarget(NamedBuildTarget target)
        {
            string[] s;
            PlayerSettings.GetScriptingDefineSymbols(target, out s);
            List<string> ls = new List<string>(s);
            ls.Remove("UNITYX_WECHAT");
            if (Enabled)
            {
                ls.Add("UNITYX_WECHAT");
            }
            PlayerSettings.SetScriptingDefineSymbols(target, ls.ToArray());
        }
    }
}